package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.competition.CompetitionMatchMessage;
import com.mdd.common.mapper.competition.CompetitionMatchMessageMapper;
import com.mdd.front.service.ICompetitionMatchMessageService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.member.MemberSearchValidate;
import com.mdd.front.vo.message.MessageListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class CompetitionMatchMessageServiceImpl extends ServiceImpl<CompetitionMatchMessageMapper, CompetitionMatchMessage> implements ICompetitionMatchMessageService {

    @Override
    public PageResult<MessageListVo> listMessage(PageValidate pageValidate, MemberSearchValidate valicate) {
        CompetitionMatchMessage query = new CompetitionMatchMessage();
        BeanUtils.copyProperties(valicate,query);
        Page page = new Page<>(pageValidate.getPage_no(), pageValidate.getPage_size());
        page = baseMapper.selectMessageList(page,query);
        List<MessageListVo> list = new LinkedList<>();
        for(Object item : page.getRecords()) {
            MessageListVo vo = new MessageListVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(page.getTotal(), page.getCurrent(), page.getSize(), list);
    }

    @Override
    public void saveMessage(CompetitionMatchMessage message) {
        baseMapper.insert(message);
    }

    @Override
    public CompetitionMatchMessage oneLike(Integer id) {
        CompetitionMatchMessage message = baseMapper.selectById(id);
        if(ObjectUtils.isNotEmpty(message)){
            CompetitionMatchMessage message1 = new CompetitionMatchMessage();
            message1.setId(id);
            message1.setLike(message.getLike()+1);
            baseMapper.updateById(message1);
            message.setLike(message1.getLike());
        }
        return message;
    }

    @Override
    public CompetitionMatchMessage disLike(Integer id) {
        CompetitionMatchMessage message = baseMapper.selectById(id);
        if(ObjectUtils.isNotEmpty(message)){
            CompetitionMatchMessage message1 = new CompetitionMatchMessage();
            message1.setId(id);
            message1.setDisLike(message.getDisLike()+1);
            baseMapper.updateById(message1);
            message.setDisLike(message1.getDisLike());
        }
        return message;
    }

}
